package com.kevin.Code.String;

import org.w3c.dom.CDATASection;

/**
 * @author Vinlee Xiao
 * @Classname IntegerToRoman
 * @Description Leetcode 12. 整数转罗马数字  中等难度
 * @Date 2021/11/2 11:39
 * @Version 1.0
 */
public class IntegerToRoman {

    /**
     * @param num
     * @return
     */
    public String intToRoman(int num) {

        StringBuilder stringBuilder = new StringBuilder();

        //1.判断位数
        int mCnt = num / 1000;
        //1000的倍数
        for (int i = 0; i < mCnt; i++) {
            stringBuilder.append("M");
        }
        //求出剩余的数
        num = num - mCnt * 1000;
        //2.处理三位数
        while ((num >= 100 && num < 1000)) {
            if (num >= 900) {
                stringBuilder.append("CM");
                num = num - 900;
            } else if (num >= 500 && num < 900) {
                stringBuilder.append("D");
                num = num - 500;
            } else if (num >= 400 && num < 500) {
                stringBuilder.append("CD");
                num = num - 400;
            } else if (num >= 100 && num < 400) {
                int nCnt = num / 100;
                for (int i = 0; i < nCnt; i++) {
                    stringBuilder.append("C");
                }
                num = num - nCnt * 100;
            }
        }


        //3.处理两位数
        while (num >= 10 && num < 100) {
            if (num >= 90) {
                stringBuilder.append("XC");
                num = num - 90;
            } else if (num >= 50 && num < 90) {
                stringBuilder.append("L");
                num = num - 50;
            } else if (num >= 40 && num < 50) {
                stringBuilder.append("XL");
                num = num - 40;
            } else if (num >= 10 && num < 40) {
                int kCnt = num / 10;
                for (int i = 0; i < kCnt; i++) {
                    stringBuilder.append("X");
                }
                num = num - kCnt * 10;
            }
        }

        while (num >= 1 && num < 10) {
            if (num >= 9) {
                stringBuilder.append("IX");
                num = num - 9;
            } else if (num >= 5 && num < 9) {
                stringBuilder.append("V");
                num = num - 5;
            } else if (num >= 4 && num < 5) {
                stringBuilder.append("IV");
                num = num - 4;
            } else if (num >= 1 && num < 4) {
                int gCnt = num / 1;
                for (int i = 0; i < gCnt; i++) {
                    stringBuilder.append("I");
                }
                num = num - gCnt * 10;
            }
        }


        return stringBuilder.toString();
    }

    /**
     * @param num
     * @return
     */
    public String intToRoman1(int num) {

        StringBuilder stringBuilder = new StringBuilder();

        int[] values = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] symbols = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

        for (int i = 0; i < values.length; i++) {
            int value = values[i];
            String ch = symbols[i];

            while (num >= value) {
                num = num - value;
                stringBuilder.append(ch);
            }

            //煎枝提前退出
            if (num == 0) {
                break;
            }
        }
        return stringBuilder.toString();
    }


    public static void main(String[] args) {
        IntegerToRoman integerToRoman = new IntegerToRoman();
        String s = integerToRoman.intToRoman1(58);
        System.out.println(s);
    }
}
